package com.android.settings.ios.face;

import android.app.Activity;
import android.app.Dialog;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.android.settings.ios.R;

public class PasswordUtils {

    public static final String KEY_PSD_STR = "key_user_lock";
    public static final String KEY_FACE_ID = "key_face_id";

    public static boolean havePassword(Activity activity) {
        String str = Settings.System.getString(activity.getContentResolver(), KEY_PSD_STR);
        return str != null && str.length() > 4;
    }

    public static void createPassword(Activity activity, final Runnable okResult) {
        final PasswordInputView view = (PasswordInputView) LayoutInflater.from(activity).inflate(R.layout.os_set_password_dialog_view, null);
        view.setNewCreateMode(true);
        view.setTextTitle(activity.getString(R.string.os_face_set_password_title));
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(activity.getResources().getDimensionPixelSize(R.dimen.os_password_dialog_layout_width),
                ViewGroup.LayoutParams.WRAP_CONTENT);
        final Dialog dialog = new Dialog(activity);
        dialog.setContentView(view, params);
        dialog.show();
        view.setOnInputChangedListener(new PasswordInputView.OnInputChangedListener() {

            @Override
            public void onClose() {
                dialog.dismiss();
            }

            @Override
            public void onVerify(String str, String old, boolean matched) {
                dialog.dismiss();
                view.savePassword(str);
                if (okResult != null) {
                    okResult.run();
                }
            }
        });
    }

    public static void deletePassword(Activity activity, final Runnable okResult) {
        final PasswordInputView view = (PasswordInputView) LayoutInflater.from(activity).inflate(R.layout.os_set_password_dialog_view, null);
        view.setNewCreateMode(false);
        view.setTextTitle(activity.getString(R.string.os_face_close_password_title));
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(activity.getResources().getDimensionPixelSize(R.dimen.os_password_dialog_layout_width),
                ViewGroup.LayoutParams.WRAP_CONTENT);
        final Dialog dialog = new Dialog(activity);
        dialog.setContentView(view, params);
        dialog.show();
        view.setOnInputChangedListener(new PasswordInputView.OnInputChangedListener() {

            @Override
            public void onClose() {
                dialog.dismiss();
            }

            @Override
            public void onVerify(String str, String old, boolean matched) {
                dialog.dismiss();
                view.deletePassword(str);
                if (okResult != null) {
                    okResult.run();
                }
            }
        });
    }

    public static void verifyPassword(Activity activity, String title, final Runnable okResult, final Runnable failResult) {
        final PasswordInputView view = (PasswordInputView) LayoutInflater.from(activity).inflate(R.layout.os_set_password_dialog_view, null);
        view.setNewCreateMode(false);
        view.setTextTitle(title);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(activity.getResources().getDimensionPixelSize(R.dimen.os_password_dialog_layout_width),
                ViewGroup.LayoutParams.WRAP_CONTENT);
        final Dialog dialog = new Dialog(activity);
        dialog.setContentView(view, params);
        dialog.show();
        view.setOnInputChangedListener(new PasswordInputView.OnInputChangedListener() {

            @Override
            public void onClose() {
                dialog.dismiss();
            }

            @Override
            public void onVerify(String str, String old, boolean matched) {
                String psd = Settings.System.getString(activity.getContentResolver(), KEY_PSD_STR);
                if (psd != null && psd.equals(str)) {
                    dialog.dismiss();
                    if (okResult != null) {
                        okResult.run();
                    }
                } else {
                    view.resetAll();
                    view.setTextSummary(activity.getString(R.string.lockpassword_invalid_password));
                    if (failResult != null) {
                        failResult.run();
                    }
                }
            }
        });
    }
}
